// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: MIT

import { Palette } from "std-widgets.slint";
import { UsecasesPalette } from "styling.slint";

// copied from common that is not public now
component LineEditBase inherits Rectangle {
    in property <string> placeholder-text;
    in property <length> font-size <=> text-input.font-size;
    in_out property <string> text <=> text-input.text;
    in property <brush> placeholder-color;
    in property <bool> enabled <=> text-input.enabled;
    in property <bool> has-focus: text-input.has-focus;
    in property <InputType> input-type <=> text-input.input-type;
    in property <TextHorizontalAlignment> horizontal-alignment <=> text-input.horizontal-alignment;
    in property <bool> read-only <=> text-input.read-only;
    in property <int> font-weight <=> text-input.font-weight;
    in property <brush> text-color;
    in property <color> selection-background-color <=> text-input.selection-background-color;
    in property <color> selection-foreground-color <=> text-input.selection-foreground-color;
    in property <length> margin;

    callback accepted( /* text */ string);
    callback edited(/* text */ string);

    public function set-selection-offsets(start: int, end: int) {
        text-input.set-selection-offsets(start, end);
    }

    public function select-all() {
        text-input.select-all();
    }

    public function clear-selection() {
        text-input.clear-selection();
    }

    public function cut() {
        text-input.cut();
    }

    public function copy() {
        text-input.copy();
    }

    public function paste() {
        text-input.paste();
    }

    min-height: text-input.preferred-height;
    min-width: max(50px, placeholder.min-width);
    clip: true;
    forward-focus: text-input;

    placeholder := Text {
        width: 100%;
        height: 100%;
        vertical-alignment: center;
        text: (root.text == "" && text-input.preedit-text == "") ? root.placeholder-text : "";
        font-size:  text-input.font-size;
        font-italic: text-input.font-italic;
        font-weight: text-input.font-weight;
        font-family: text-input.font-family;
        color: root.placeholder-color;
        horizontal-alignment: root.horizontal-alignment;
    }

    text-input := TextInput {
        property <length> computed-x;

        x: min(0px, max(parent.width - self.width - self.text-cursor-width, self.computed-x));
        width: max(parent.width - self.text-cursor-width, self.preferred-width);
        height: 100%;
        vertical-alignment: center;
        single-line: true;
        color: root.text-color;

        cursor-position-changed(cpos) => {
            if (cpos.x + self.computed_x < root.margin) {
                self.computed_x = - cpos.x + root.margin;
            } else if (cpos.x + self.computed_x > parent.width - root.margin - self.text-cursor-width) {
                self.computed_x = parent.width - cpos.x - root.margin - self.text-cursor-width;
            }
        }

        accepted => { root.accepted(self.text); }

        edited => { root.edited(self.text); }
    }
}

export component ExtendedLineEdit {
    in property <bool> enabled <=> base.enabled;
    in property <InputType> input-type <=> base.input-type;
    in property <TextHorizontalAlignment> horizontal-alignment <=> base.horizontal-alignment;
    in property <bool> read-only <=> base.read-only;
    in property <length> font-size <=> base.font-size;
    in property <string> placeholder-text <=> base.placeholder-text;
    out property <bool> has-focus <=> base.has-focus;
    in-out property <string> text <=> base.text;

    property <length> style-min-height: UsecasesPalette.use-material ? 56px : 32px;

    callback accepted <=> base.accepted;
    callback edited <=> base.edited;
    accessible-role: text-input;
    accessible-value <=> text;

    public function set-selection-offsets(start: int, end: int) {
        base.set-selection-offsets(start, end);
    }

    public function select-all() {
        base.select-all();
    }

    public function clear-selection() {
        base.clear-selection();
    }

    public function cut() {
        base.cut();
    }

    public function copy() {
        base.copy();
    }

    public function paste() {
        base.paste();
    }

    vertical-stretch: 0;
    horizontal-stretch: 1;
    min-width: max(160px, layout.min-width);
    min-height: max(root.style-min-height, layout.min-height);
    forward-focus: base;

    states [
        disabled when !root.enabled : {
            root.opacity: 0.5;
        }
    ]

    background := Rectangle {
        border-radius: UsecasesPalette.use-material ? 4px : 8px;
        background: UsecasesPalette.use-material ? transparent : Palette.control-background;
        border-width: root.has-focus && UsecasesPalette.use-material ? 2px :  1px;
        border-color: UsecasesPalette.control-divider;

        layout := HorizontalLayout {
            padding-left: 16px;
            padding-right: 16px;
            spacing: 8px;

            HorizontalLayout {
                @children
            }

            base := LineEditBase {
                font-size: 15 * 0.0769rem;
                font-weight: 400;
                selection-background-color: Palette.selection-background;
                selection-foreground-color: Palette.accent-foreground;
                text-color: Palette.foreground;
                placeholder-color: UsecasesPalette.placeholder-foreground;
                margin: layout.padding-left + layout.padding-right;
            }
        }

         if root.has-focus && root.enabled : Rectangle {
            width: parent.width + 2px;
            height: parent.height + 2px;
            border-radius: parent.border-radius + 2px;
            border-color: UsecasesPalette.state-focus;
            border-width: 1px;
        }
    }
}
